package org.apache.commons.httpclient;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.util.EncodingUtil;
import org.apache.commons.httpclient.util.ExceptionUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ChunkedInputStream extends InputStream
{
  private static final Log LOG;
  static Class class$org$apache$commons$httpclient$ChunkedInputStream;
  private boolean bof;
  private int chunkSize;
  private boolean closed;
  private boolean eof;
  private InputStream in;
  private HttpMethod method;
  private int pos;

  static
  {
    Class localClass;
    if (class$org$apache$commons$httpclient$ChunkedInputStream == null)
    {
      localClass = class$("org.apache.commons.httpclient.ChunkedInputStream");
      class$org$apache$commons$httpclient$ChunkedInputStream = localClass;
    }
    while (true)
    {
      LOG = LogFactory.getLog(localClass);
      return;
      localClass = class$org$apache$commons$httpclient$ChunkedInputStream;
    }
  }

  public ChunkedInputStream(InputStream paramInputStream)
    throws IOException
  {
    this(paramInputStream, null);
  }

  public ChunkedInputStream(InputStream paramInputStream, HttpMethod paramHttpMethod)
    throws IOException
  {
    this.bof = true;
    this.eof = false;
    this.closed = false;
    this.method = null;
    if (paramInputStream == null)
      throw new IllegalArgumentException("InputStream parameter may not be null");
    this.in = paramInputStream;
    this.method = paramHttpMethod;
    this.pos = 0;
  }

  static Class class$(String paramString)
  {
    try
    {
      Class localClass = Class.forName(paramString);
      return localClass;
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
    }
  }

  static void exhaustInputStream(InputStream paramInputStream)
    throws IOException
  {
    byte[] arrayOfByte = new byte[1024];
    while (paramInputStream.read(arrayOfByte) >= 0);
  }

  private static int getChunkSizeFromInputStream(InputStream paramInputStream)
    throws IOException
  {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    label10: int l;
    for (int i = 0; ; i = -1)
    {
      if (i == -1)
        break label205;
      l = paramInputStream.read();
      if (l == -1)
        throw new IOException("chunked stream ended unexpectedly");
      switch (i)
      {
      default:
        throw new RuntimeException("assertion failed");
      case 0:
        switch (l)
        {
        default:
        case 13:
        case 34:
        }
        while (true)
        {
          localByteArrayOutputStream.write(l);
          break label10:
          i = 1;
          break label10:
          i = 2;
        }
      case 1:
        if (l != 10)
          break;
      case 2:
      }
    }
    throw new IOException("Protocol violation: Unexpected single newline character in chunk size");
    switch (l)
    {
    default:
    case 92:
    case 34:
    }
    while (true)
    {
      localByteArrayOutputStream.write(l);
      break label10:
      localByteArrayOutputStream.write(paramInputStream.read());
      break label10:
      i = 0;
    }
    label205: String str1 = EncodingUtil.getAsciiString(localByteArrayOutputStream.toByteArray());
    int j = str1.indexOf(59);
    String str2;
    if (j > 0)
      str2 = str1.substring(0, j).trim();
    while (true)
      try
      {
        int k = Integer.parseInt(str2.trim(), 16);
        return k;
        str2 = str1.trim();
      }
      catch (NumberFormatException localNumberFormatException)
      {
        throw new IOException("Bad chunk size: " + str2);
      }
  }

  private void nextChunk()
    throws IOException
  {
    if (!(this.bof))
      readCRLF();
    this.chunkSize = getChunkSizeFromInputStream(this.in);
    this.bof = false;
    this.pos = 0;
    if (this.chunkSize != 0)
      return;
    this.eof = true;
    parseTrailerHeaders();
  }

  private void parseTrailerHeaders()
    throws IOException
  {
    try
    {
      String str = "US-ASCII";
      if (this.method != null)
        str = this.method.getParams().getHttpElementCharset();
      Header[] arrayOfHeader = HttpParser.parseHeaders(this.in, str);
      if (this.method == null)
        return;
      int i = 0;
      if (i >= arrayOfHeader.length)
        return;
      this.method.addResponseFooter(arrayOfHeader[i]);
      ++i;
    }
    catch (HttpException localHttpException)
    {
      LOG.error("Error parsing trailer headers", localHttpException);
      IOException localIOException = new IOException(localHttpException.getMessage());
      ExceptionUtil.initCause(localIOException, localHttpException);
      throw localIOException;
    }
  }

  private void readCRLF()
    throws IOException
  {
    int i = this.in.read();
    int j = this.in.read();
    if ((i == 13) && (j == 10))
      return;
    throw new IOException("CRLF expected at end of chunk: " + i + "/" + j);
  }

  public void close()
    throws IOException
  {
    if (!(this.closed));
    try
    {
      if (!(this.eof))
        exhaustInputStream(this);
      return;
    }
    finally
    {
      this.eof = true;
      this.closed = true;
    }
  }

  public int read()
    throws IOException
  {
    if (this.closed)
      throw new IOException("Attempted read from closed stream.");
    if (this.eof);
    for (int i = -1; ; i = this.in.read())
    {
      while (true)
      {
        return i;
        if (this.pos < this.chunkSize)
          break;
        nextChunk();
        if (!(this.eof))
          break;
        i = -1;
      }
      this.pos = (1 + this.pos);
    }
  }

  public int read(byte[] paramArrayOfByte)
    throws IOException
  {
    return read(paramArrayOfByte, 0, paramArrayOfByte.length);
  }

  public int read(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
    throws IOException
  {
    if (this.closed)
      throw new IOException("Attempted read from closed stream.");
    if (this.eof);
    int j;
    for (int k = -1; ; k = j)
    {
      while (true)
      {
        return k;
        if (this.pos < this.chunkSize)
          break;
        nextChunk();
        if (!(this.eof))
          break;
        k = -1;
      }
      int i = Math.min(paramInt2, this.chunkSize - this.pos);
      j = this.in.read(paramArrayOfByte, paramInt1, i);
      this.pos = (j + this.pos);
    }
  }
}

/* Location:           E:\Desktop\Android\wordstd\classes_dex2jar.jar
 * Qualified Name:     org.apache.commons.httpclient.ChunkedInputStream
 * JD-Core Version:    0.5.3
 */